/*
 * Copyright (C) 2013～2023 上海颐凡软件科技有限公司
 * Yfann Software Technology (Shanghai) Co.,LTD
 * All Rights Reserved.
 * 公司网址: www.yfann.com
 * 365IT教育网，成就您优秀架构师之梦！
 * 平台网址: www.365itedu.com
 */
package com.yfann.web.base.sys.filter;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.yfann.sword.web.fwk.consts.FrameworkConst;
import com.yfann.sword.web.fwk.util.RequestUtil;
import com.yfann.web.base.entity.dto.TSYS01OutputDto;

/**
 * 用户访问URL权限控制类。
 * 
 * <pre>
 * 修改履历
 * -----------------------------------------------------------------------------
 *         VERSION       DATE            BY       CHANGE/COMMENT
 * -----------------------------------------------------------------------------
 *          1.0         2013/10/11      颜廷吉            create
 * -----------------------------------------------------------------------------
 * </pre>
 */
public class CodeInitFilter implements Filter {
    /**
     * 过滤器上下文
     */
    private FilterConfig filterConfig;

    /**
     * Filter初始化。
     * @param filterConfig 过滤器
     */
    @Override
    public void init(FilterConfig filterConfig) {
        this.filterConfig = filterConfig;
    }

    /**
     * 日志过滤器处理。<br>
     * @param request 请求
     * @param response 响应
     * @param chain 过滤器链
     * @throws ServletException IOException
     * @throws IOException IOException
     */
    @SuppressWarnings("unchecked")
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        if (filterConfig == null) {
            return;
        }

        if (request instanceof HttpServletRequest) {
            HttpServletRequest servletRequest = (HttpServletRequest) request;
            HttpSession session = servletRequest.getSession();
            ServletContext servletContext = session.getServletContext();
            List<TSYS01OutputDto> codeBeanList = (List<TSYS01OutputDto>) servletContext.getAttribute(FrameworkConst.CODE_BEAN_LIST_KEY);

            // request里根据用户语言信息设定静态编码信息
            String i18LanguageName = RequestUtil.getUserI18LanguageName(servletRequest);
            HashMap<String, TreeMap<String, String>> codeHashMap = getCodeHashMap(i18LanguageName, codeBeanList);
            Iterator<String> it = codeHashMap.keySet().iterator();
            while (it.hasNext()) {
                String key = (String) it.next();
                servletRequest.setAttribute(key, codeHashMap.get(key));
            }
            
            // 动态CODE：角色
            servletRequest.setAttribute("DYNCD001", session.getAttribute("DYNCD001"));
        }
        

        chain.doFilter(request, response);
    }

    /**
     * 销毁日志过滤器。
     */
    @Override
    public void destroy() {
        this.filterConfig = null;
    }

    /**
     * 取得CODE编码处理。
     * @return HashMap<String, TreeMap<String, String>>
     */
    private HashMap<String, TreeMap<String, String>> getCodeHashMap(String i18LanguageName, List<TSYS01OutputDto> codeBeanList) {
        HashMap<String, TreeMap<String, String>> codeHashMap = new HashMap<String, TreeMap<String, String>>();
        if (codeBeanList != null && i18LanguageName != null) {
            for (TSYS01OutputDto codeBean : codeBeanList) {
                if (i18LanguageName.equals(codeBean.getCodeLg())) {
                    String codeID = codeBean.getCodeId();
                    String codeItem = codeBean.getCodeItem();
                    String codeName = codeBean.getCodeName();
                    Iterator<String> it = codeHashMap.keySet().iterator();
                    boolean hasKey = false;
                    while (it.hasNext()) {
                        String key;
                        key = (String) it.next();
                        if (codeBean.getCodeId().equals(key)) {
                            hasKey = true;
                            break;
                        }
                    }
                    if (hasKey) {
                        codeHashMap.get(codeID).put(codeItem, codeName);
                    } else {
                        codeHashMap.put(codeID, new TreeMap<String, String>());
                        codeHashMap.get(codeID).put(codeItem, codeName);
                    }
                }
            }
        }
        return codeHashMap;
    }
}
